angular.module("busesApp",['ui.bootstrap','httpService']).
controller("busesCtrl",['$scope','$http','$uibModal','$timeout','busesService','busOrderService','routesService',
function($scope,$http,$modal,$timeout,busesService,busOrderService,routesService){
  $scope.addBuses=function(){
      $modal.open({
          templateUrl:'modifyBuses',
          controller:'insertBusesCtrl',
          backdrop:'static'
      });
  };
  $scope.checkedIndex = -1;
  $scope.selectChange=function(index){
       if($scope.buses[index].checked==true){
            $scope.buses[index].checked=false;
          }
       else{
            $scope.buses[index].checked=true;
           }
        if($scope.buses[index].checked){
            $scope.checkedIndex = index;
            return;
        }
        for(var i = 0; i < $scope.buses.length; i++){
            var item = $scope.buses[i];
            if(item.checked != undefined && item.checked == true){
                $scope.checkedIndex = i;
                return;
            }
        }
        $scope.checkedIndex = -1;
  };
  $scope.refreshBuses=function(){
      busesService.getInfoByCount({start:0,count:50}).then(function(result){
          $scope.buses=result;
      });
  };
  $scope.refreshBuses();
  $scope.deleteBuses=function(){
        if($scope.checkedIndex==-1){
            alert("请选择");
            return;
        }
        if(confirm("是否删除车次,如果已有座位订票则不许删除!")){
            busesService.delete({"id":$scope.buses[$scope.checkedIndex].id}).then(function(result){
                alert("删除成功");
                $scope.refreshBuses();
            });
        }
  };
  $scope.updateBuses=function(){
       if($scope.checkedIndex==-1){
            alert("请选择");
            return;
        }
      $modal.open({
          templateUrl:'modifyBuses',
          scope:$scope,
          controller:'updateBusesCtrl'
      });
  };
}]).
controller("insertBusesCtrl",['$scope','$uibModalInstance','busesService','busOrderService','routesService',
function($scope,$uibModalInstance,busesService,busOrderService,routesService){
    busOrderService.getByCount({start:0,count:15}).then(function(result){
         angular.forEach(result,function(item){
            if(item.orientation==0){
                item.orientation='成都方向';
            }
            else{
                item.orientation='基地方向';
            }
        });
        $scope.busOrders=result;
    });
    routesService.getAll().then(function(result){       
        $scope.routes=result;        
    });
    $scope.submit=function(){
        $scope.loading=true;
        busesService.insert($scope.bus).then(function(){
            $scope.loading=false;
            $scope.submitted=true;
            $scope.result="成功";
        },function(){
            $scope.loading=false;
            $scope.submitted=true;
            $scope.result="失败";
        });
    };
    $scope.close=function(){
        $uibModalInstance.close();
    };
}]).
controller("updateBusesCtrl",['$scope','$uibModalInstance','busesService','busOrderService','routesService',
function($scope,$uibModalInstance,busesService,busOrderService,routesService){
    busOrderService.getByCount({start:0,count:15}).then(function(result){
         angular.forEach(result,function(item){
            if(item.orientation==0){
                item.orientation='成都方向';
            }
            else{
                item.orientation='基地方向';
            }
        });
        $scope.busOrders=result;
    });
    routesService.getAll().then(function(result){       
        $scope.routes=result;        
    });
    $scope.bus=$scope.buses[$scope.checkedIndex];
    $scope.submit=function(){
        $scope.loading=true;
        var data={};
        data.id= $scope.bus.id;
        data.routeid= $scope.bus.routeid;
        data.busorderid= $scope.bus.busorderid;
        data.number= $scope.bus.number;
        data.seatcount= $scope.bus.seatcount;
        data.other= $scope.bus.other;
        busesService.update(data).then(function(){
            $scope.loading=false;
            $scope.submitted=true;
            $scope.result="成功";
            $scope.refreshBuses();
        },function(){
            $scope.loading=false;
            $scope.submitted=true;
            $scope.result="失败";
        });
    };
    $scope.close=function(){
        $uibModalInstance.close();
    };
}]);